package com.zj.study.strategy1;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author 赵赳
 * @since 2022/7/26 15:57
 */
@Component
public class PaymentStrategy {

  private final static Map<PaymentEnum, PaymentService> PAYMENT_SERVICE_MAP = new ConcurrentHashMap<>();

  private AliPayService aliPayService;
  private WechatPayService wechatPayService;


  @Autowired
  public void setAliPayService(AliPayService aliPayService) {
    this.aliPayService = aliPayService;
  }

  @Autowired
  public void setWechatPayService(WechatPayService wechatPayService) {
    this.wechatPayService = wechatPayService;
  }

  /**
   * 初始化支付方方式实现类
   */
  @PostConstruct
  public void initPaymentService() {
    PAYMENT_SERVICE_MAP.put(PaymentEnum.ALIPAY, aliPayService);
    PAYMENT_SERVICE_MAP.put(PaymentEnum.WECHATPAY, wechatPayService);
  }

  /**
   * 获取支付实现类
   *
   * @param paymentEnum 支付方式枚举
   * @return 支付方式
   */
  public PaymentService getPaymentService(PaymentEnum paymentEnum) {
    return PAYMENT_SERVICE_MAP.get(paymentEnum);
  }


}
